package com.sid.oncf.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sid.oncf.dao.interfaces.IUserDao;
import com.sid.oncf.entity.Users;

public class UserDaoImpl implements IUserDao {
	
	@PersistenceContext
	private EntityManager em;

	public boolean save(Users u) {
		em.persist(u);
		return true;
	}

	public boolean update(Users u) {
		em.merge(u);
		return true;
	}

	public boolean delete(int id) {
		em.remove(find(id));
		return true;
	}

	public Users find(int id) {
		return em.find(Users.class, id);
	}

	public Users findByUserName(String username) {
		Query query=em.createQuery("select u from Users u where u.login=:login");
		query.setParameter("login", username);
		Users user=(Users) query.getSingleResult();
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<Users> findAll() {
		Query query=em.createQuery("select u from Users u");
		return query.getResultList();
	}

}
